package org.budo.graph.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.budo.graph.Constant;
import org.budo.graph.entity.AppGraph;
import org.budo.graph.entity.CacheGraph;
import org.budo.graph.entity.SpringGraph;
import org.budo.graph.entity.SqlGraph;
import org.budo.graph.entity.UrlGraph;
import org.budo.support.lang.util.ListUtil;
import org.budo.support.lang.util.MapUtil;

import com.alibaba.druid.support.json.JSONUtils;

/**
 * @author limingwei
 */
public class EchartsUtil implements Constant.Repository {
    protected static Map<String, Object> toEchartsData(Map<Double, Object> zrangeWithScores, String[] metaKeyArray) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        for (Entry<Double, Object> entry : zrangeWithScores.entrySet()) {
            Object value = entry.getValue();
            List<Object> valueArray = (List<Object>) JSONUtils.parse(value + "");

            double score = entry.getKey();
            long scoreLong = (long) score;
            dataMap.put(scoreLong + "", valueArray);
        }

        List<String> metaKeys = ListUtil.toList(metaKeyArray);
        return MapUtil.stringObjectMap("meta", metaKeys, //
                "data", dataMap);
    }

    protected static String[] graphTitles(Map<String, String> parameters) {
        String type = parameters.get(PARAMETER_TYPE);

        if (UrlGraph.GRAPH_TYPE.equalsIgnoreCase(type)) {
            return UrlGraph.TITLES;
        }

        if (CacheGraph.GRAPH_TYPE.equalsIgnoreCase(type)) {
            return CacheGraph.TITLES;
        }

        if (SpringGraph.GRAPH_TYPE.equalsIgnoreCase(type)) {
            return SpringGraph.TITLES;
        }

        if (SqlGraph.GRAPH_TYPE.equalsIgnoreCase(type)) {
            return SqlGraph.TITLES;
        }

        return AppGraph.TITLES;
    }
}